﻿Imports ICtagControl.frmStartCompleteBase

Public Class clsDbMold

    ' 金型検査履歴テーブル取得処理
    Public Function GetMoldInspection(ByVal procsCD As String, ByVal equipId As String) As DataTable
        Dim szSql As String = ""

        szSql &= " SELECT "
        szSql &= "  PROC_CD, EQUIP_NO, MOLD_MODEL, MOLD_LOT_NO, INSPECTION_DATE_TIME "
        szSql &= " FROM PER.L_MOLD_INSPECTION_HIST "
        szSql &= " WHERE PROC_CD = ? AND EQUIP_NO = ? "

        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, procsCD, equipId)
        If IsNothing(dt) Then
            GetMoldInspection = New DataTable
        Else
            GetMoldInspection = dt
        End If
    End Function

    ' 金型検査履歴更新処理
    Public Function updateMoldInspection(ByVal procsCD As String, ByVal equipId As String, ByVal mold As String, ByVal moldLotNo As String, ByVal dateTime As String) As Integer

        Dim delCnt As Integer = 0
        Dim insCnt As Integer = 0

        'トランザクション開始
        DBacs.BeginTransaction()

        Dim delSql As String = ""
        delSql &= " DELETE "
        delSql &= " FROM PER.L_MOLD_INSPECTION_HIST "
        delSql &= " WHERE PROC_CD= ? "
        delSql &= " AND EQUIP_NO= ? "

        delCnt = DBacs.ExecuteUpdateSql(delSql, -1, procsCD, equipId)

        If delCnt < 0 Then
            '削除エラーはロールバックする
            DBacs.RollbackTransaction()
            Return 0
        End If

        '更新SQL
        Dim insSql As String = ""
        insSql &= " INSERT INTO PER.L_MOLD_INSPECTION_HIST ( "
        insSql &= "   PROC_CD, EQUIP_NO, MOLD_MODEL, MOLD_LOT_NO, INSPECTION_DATE_TIME "
        insSql &= " ) "
        insSql &= " VALUES ( ?, ?, ?, ?, ? ) "

        insCnt = DBacs.ExecuteUpdateSql(insSql, -1, procsCD, equipId, mold, moldLotNo, CDate(dateTime))

        If insCnt < 0 Then
            '登録エラーはロールバックする
            DBacs.RollbackTransaction()
            Return 0
        End If

        DBacs.CommitTransaction()

        Return 1
    End Function


    ' 金型検査履歴更新処理(複数金型登録)
    Public Function updateMoldInspectionCut(ByVal procsCD As String, ByVal equipId As String, ByVal mold As DataTable, ByVal dateTime As String) As Integer

        Dim delCnt As Integer = 0
        Dim insCnt As Integer = 0

        'トランザクション開始
        DBacs.BeginTransaction()

        Dim delSql As String = ""
        delSql &= " DELETE "
        delSql &= " FROM PER.L_MOLD_INSPECTION_HIST "
        delSql &= " WHERE PROC_CD= ? "
        delSql &= " AND EQUIP_NO= ? "

        delCnt = DBacs.ExecuteUpdateSql(delSql, -1, procsCD, equipId)

        If delCnt < 0 Then
            '削除エラーはロールバックする
            DBacs.RollbackTransaction()
            Return 0
        End If

        '登録SQL
        Dim insSql As String = ""
        For i = 0 To mold.Rows.Count - 1
            With mold.Rows(i)
                insSql = "" 'ADD MIAO 2018/10/23
                insSql &= " INSERT INTO PER.L_MOLD_INSPECTION_HIST ( "
                insSql &= "   PROC_CD, EQUIP_NO, MOLD_MODEL, MOLD_LOT_NO, INSPECTION_DATE_TIME "
                insSql &= " ) "
                insSql &= " VALUES ("
                insSql &= String.Format(" '{0}', '{1}', '{2}', '{3}', '{4}'",
                                       procsCD, equipId, .Item("MOLD_MODEL"), .Item("MOLD_LOT_NO"), CDate(dateTime))
                insSql &= " ); " '& vbCrLf DEL MIAO 2018/10/23
                'ADD MIAO 2018/10/23 BEGIN
                insCnt = DBacs.ExecuteUpdateSql(insSql, -1)
                If insCnt < 0 Then
                    '登録エラーはロールバックする
                    DBacs.RollbackTransaction()
                    Return 0
                End If
                'ADD MIAO 2018/10/23 END
            End With
        Next
        'DEL MIAO 2018/10/23 BEGIN
        'If Not insSql = "" Then
        'insCnt = DBacs.ExecuteUpdateSql(insSql, -1)

        'If insCnt < 0 Then
        '    '登録エラーはロールバックする
        '    DBacs.RollbackTransaction()
        '    Return 0
        'End If
        'End If
        'DEL MIAO 2018/10/23 END
        DBacs.CommitTransaction()
        Return 1

    End Function
End Class
